/* 
 * Copyright 2005-2010 Samuel Mello & Eduardo Schnell
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; version 2 or later of the License.
 * 
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 * 
 */

/*
 * ChordEditorFrame.java
 *
 * Created on 23 de Janeiro de 2006, 20:34
 */

package datasoul.serviceitems.song;

import java.util.ArrayList;

import javax.swing.JOptionPane;

import datasoul.DatasoulMainForm;

/**
 *
 * @author  Administrador
 */
public class ChordEditorForm extends javax.swing.JFrame {

    /**
	 * 
	 */
	private static final long serialVersionUID = -7287724964857848396L;
	private Chord chord;
    private ChordsDB chordsDB;
    private boolean isNewChord;
    
    /** Creates new form ChordEditorFrame */
    public ChordEditorForm(ChordsDB chordsDB, Chord chord) {
        DatasoulMainForm.setDatasoulIcon(this);
        if(chord==null){
            this.chord = new Chord();            
            isNewChord = true;
        }else{
            this.chord = chord;
            isNewChord = false;
        }
        this.chordsDB = chordsDB;
        
        initComponents();
        
        
        ArrayList<String> shapes = this.chord.getShapes();
        if(shapes.size()>0)
            this.chordShapePanel1.setShape(shapes.get(0));
        if(shapes.size()>1)
            this.chordShapePanel2.setShape(shapes.get(1));
        if(shapes.size()>2)
            this.chordShapePanel3.setShape(shapes.get(2));
        if(shapes.size()>3)
            this.chordShapePanel4.setShape(shapes.get(3));
        if(shapes.size()>4)
            this.chordShapePanel5.setShape(shapes.get(4));
        if(shapes.size()>5)
            this.chordShapePanel6.setShape(shapes.get(5));
        
        this.fieldName.setText(this.chord.getName());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        chordShapePanel1 = new datasoul.serviceitems.song.ChordShapePanel();
        chordShapePanel2 = new datasoul.serviceitems.song.ChordShapePanel();
        chordShapePanel3 = new datasoul.serviceitems.song.ChordShapePanel();
        chordShapePanel4 = new datasoul.serviceitems.song.ChordShapePanel();
        chordShapePanel5 = new datasoul.serviceitems.song.ChordShapePanel();
        chordShapePanel6 = new datasoul.serviceitems.song.ChordShapePanel();
        fieldName = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        btnApply = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("datasoul/internationalize"); // NOI18N
        setTitle(bundle.getString("EDIT CHORD")); // NOI18N
        setResizable(false);

        chordShapePanel1.setToolTipText(bundle.getString("SELECT THE NOTES CLICKING")); // NOI18N

        javax.swing.GroupLayout chordShapePanel1Layout = new javax.swing.GroupLayout(chordShapePanel1);
        chordShapePanel1.setLayout(chordShapePanel1Layout);
        chordShapePanel1Layout.setHorizontalGroup(
            chordShapePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 88, Short.MAX_VALUE)
        );
        chordShapePanel1Layout.setVerticalGroup(
            chordShapePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 150, Short.MAX_VALUE)
        );

        chordShapePanel2.setToolTipText(bundle.getString("SELECT THE NOTES CLICKING")); // NOI18N

        javax.swing.GroupLayout chordShapePanel2Layout = new javax.swing.GroupLayout(chordShapePanel2);
        chordShapePanel2.setLayout(chordShapePanel2Layout);
        chordShapePanel2Layout.setHorizontalGroup(
            chordShapePanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 90, Short.MAX_VALUE)
        );
        chordShapePanel2Layout.setVerticalGroup(
            chordShapePanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 150, Short.MAX_VALUE)
        );

        chordShapePanel3.setToolTipText(bundle.getString("SELECT THE NOTES CLICKING")); // NOI18N

        javax.swing.GroupLayout chordShapePanel3Layout = new javax.swing.GroupLayout(chordShapePanel3);
        chordShapePanel3.setLayout(chordShapePanel3Layout);
        chordShapePanel3Layout.setHorizontalGroup(
            chordShapePanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 95, Short.MAX_VALUE)
        );
        chordShapePanel3Layout.setVerticalGroup(
            chordShapePanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 150, Short.MAX_VALUE)
        );

        chordShapePanel4.setToolTipText(bundle.getString("SELECT THE NOTES CLICKING")); // NOI18N

        javax.swing.GroupLayout chordShapePanel4Layout = new javax.swing.GroupLayout(chordShapePanel4);
        chordShapePanel4.setLayout(chordShapePanel4Layout);
        chordShapePanel4Layout.setHorizontalGroup(
            chordShapePanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 88, Short.MAX_VALUE)
        );
        chordShapePanel4Layout.setVerticalGroup(
            chordShapePanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 150, Short.MAX_VALUE)
        );

        chordShapePanel5.setToolTipText(bundle.getString("SELECT THE NOTES CLICKING")); // NOI18N

        javax.swing.GroupLayout chordShapePanel5Layout = new javax.swing.GroupLayout(chordShapePanel5);
        chordShapePanel5.setLayout(chordShapePanel5Layout);
        chordShapePanel5Layout.setHorizontalGroup(
            chordShapePanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 95, Short.MAX_VALUE)
        );
        chordShapePanel5Layout.setVerticalGroup(
            chordShapePanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 150, Short.MAX_VALUE)
        );

        chordShapePanel6.setToolTipText(bundle.getString("SELECT THE NOTES CLICKING")); // NOI18N

        javax.swing.GroupLayout chordShapePanel6Layout = new javax.swing.GroupLayout(chordShapePanel6);
        chordShapePanel6.setLayout(chordShapePanel6Layout);
        chordShapePanel6Layout.setHorizontalGroup(
            chordShapePanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 90, Short.MAX_VALUE)
        );
        chordShapePanel6Layout.setVerticalGroup(
            chordShapePanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 150, Short.MAX_VALUE)
        );

        jLabel1.setText(bundle.getString("CHORD")); // NOI18N

        btnApply.setIcon(new javax.swing.ImageIcon(getClass().getResource("/datasoul/icons/v2/dialog-apply.png"))); // NOI18N
        btnApply.setText(bundle.getString("APPLY")); // NOI18N
        btnApply.setToolTipText(bundle.getString("APPLY CHORD CHANGES")); // NOI18N
        btnApply.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnApplyMouseClicked(evt);
            }
        });

        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/datasoul/icons/v2/process-stop.png"))); // NOI18N
        btnCancel.setText(bundle.getString("CANCEL")); // NOI18N
        btnCancel.setToolTipText(bundle.getString("CANCEL AND CLOSE THE WINDOW")); // NOI18N
        btnCancel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnCancelMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(fieldName, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(chordShapePanel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(chordShapePanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(chordShapePanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(btnApply, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(chordShapePanel6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnCancel, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(chordShapePanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(chordShapePanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(fieldName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(chordShapePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(chordShapePanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(chordShapePanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(chordShapePanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(chordShapePanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(chordShapePanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnApply))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnCancelMouseClicked
        this.dispose();
    }//GEN-LAST:event_btnCancelMouseClicked

    private void btnApplyMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnApplyMouseClicked
        chord.removeAllShapes();

        if(this.fieldName.getText().equals("")){
            JOptionPane.showMessageDialog(this,java.util.ResourceBundle.getBundle("datasoul/internationalize").getString("PLEASE FILL THE CHORD NAME, IT IS EMPTY"));
            return;
        }

        chord.setName(this.fieldName.getText().replace(" ",""));
        
        if(!this.chordShapePanel1.isEmpty())
            chord.addShape(this.chordShapePanel1.getShape());
        if(!this.chordShapePanel2.isEmpty())
                chord.addShape(this.chordShapePanel2.getShape());
        if(!this.chordShapePanel3.isEmpty())
                chord.addShape(this.chordShapePanel3.getShape());
        if(!this.chordShapePanel4.isEmpty())
                chord.addShape(this.chordShapePanel4.getShape());
        if(!this.chordShapePanel5.isEmpty())
                chord.addShape(this.chordShapePanel5.getShape());
        if(!this.chordShapePanel6.isEmpty())
                chord.addShape(this.chordShapePanel6.getShape());

        if(isNewChord){
            chordsDB.addItem(this.chord);
        }

        chordsDB.sortByName();        
        
        chordsDB.save();        
        this.dispose();
    }//GEN-LAST:event_btnApplyMouseClicked
    
    /**
     * @param args the command line arguments
     */
/*    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ChordEditorFrame().setVisible(true);
            }
        });
    }
*/    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnApply;
    private javax.swing.JButton btnCancel;
    private datasoul.serviceitems.song.ChordShapePanel chordShapePanel1;
    private datasoul.serviceitems.song.ChordShapePanel chordShapePanel2;
    private datasoul.serviceitems.song.ChordShapePanel chordShapePanel3;
    private datasoul.serviceitems.song.ChordShapePanel chordShapePanel4;
    private datasoul.serviceitems.song.ChordShapePanel chordShapePanel5;
    private datasoul.serviceitems.song.ChordShapePanel chordShapePanel6;
    private javax.swing.JTextField fieldName;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
    
}


